#include "whaleavatar.h"

#include <QMouseEvent>
#include <QPixmap>
#include <QtDebug>
#include <QVBoxLayout>
#include <QIcon>
#include <QCursor>
#include <QtDebug>
#include <QTimer>
#include <QPixmapCache>
#include <QPropertyAnimation>
#include <QMainWindow>
#include "whaleavatar.h"
#include "whalebutton.h"

QStringList waitingForCache;

WhaleAvatar::WhaleAvatar( QString avatarUrl, QString userId ) : QLabel( 0 ), mAvatarUrl( avatarUrl ), mUserId( userId ) {
    this->mConnector = new QNetworkAccessManager();

    if ( this->mAvatarUrl.isEmpty() ) {
        return;
    }

    QPixmap pm;
    if ( QPixmapCache::find( this->mUserId, &pm ) ) {
        if ( pm.isNull() ) {
            this->WaitForCache();
        }
        else {
            this->setPixmap( pm );
            this->setCursor( QCursor( Qt::PointingHandCursor ) );
            QTimer::singleShot( 5, this, SLOT( update() ) );
        }
    }
    else {
        QPixmapCache::insert( this->mUserId, pm );
        this->mAvatarReply = this->mConnector->get( QNetworkRequest( QUrl( avatarUrl ) ) );
    }

    this->connect( this->mConnector, SIGNAL( finished( QNetworkReply * ) ), this, SLOT( ReadAvatar( QNetworkReply * ) ) );
}

void WhaleAvatar::WaitForCache() {
    QPixmap pm;
    QPixmapCache::find( this->mUserId, &pm );
    if ( pm.isNull() ) {
        QTimer::singleShot( 100, this, SLOT( WaitForCache() ) );
    }
    else {
        this->setPixmap( pm );
        this->setCursor( QCursor( Qt::PointingHandCursor ) );
        QTimer::singleShot( 5, this, SLOT( update() ) );
    }
}

void WhaleAvatar::ReadAvatar( QNetworkReply * reply ) {
    // qDebug() << reply->url().toString();

    QByteArray bytes = reply->readAll();

    QImage avatarImage;
    avatarImage.loadFromData( bytes );
    
    if ( reply->url().toString().indexOf( "_normal" ) != -1 ) {
        if ( avatarImage.isNull() ) {
            qDebug() << "NULL image: " << this->mAvatarUrl;
            return;
        }
        QPixmap pm = QPixmap::fromImage( avatarImage.scaled( 50, 50 ) );
        QPixmapCache::remove( this->mUserId );
        QPixmapCache::insert( this->mUserId, pm );

        this->setPixmap( pm );
        this->setCursor( QCursor( Qt::PointingHandCursor ) );
        QTimer::singleShot( 5, this, SLOT( update() ) );
        this->mImage = avatarImage;
    }
    else if ( !avatarImage.isNull() ) {
        QMainWindow * window = qobject_cast< QMainWindow * >( this->window() );
        int centerx = ( window->x() + window->x() + window->frameGeometry().width() / 2.0 );
        int centery = ( window->y() + window->y() + window->frameGeometry().height() / 2.0 );
        int imgw = avatarImage.size().width();
        int imgh = avatarImage.size().height();

        /*
        MainWindow * win = qobject_cast< MainWindow * >( window );
        */
        QWidget * central = window->centralWidget();

        QLabel * label = new QLabel( central );
        label->setPixmap( QPixmap::fromImage( avatarImage ) );
        // label->setWindowFlags( Qt::FramelessWindowHint );
        label->move( centerx - ( imgw / 2.0 ), centery - ( imgh / 2.0 ) );
        label->setObjectName( "avatarBig" );
        label->setScaledContents( true );
        label->show();
        
        QPixmap closePic;
        int halfsize;
        if ( imgw > 80 && imgh > 80 ) {
            closePic = QPixmap( ":/images/oxygen/dialog-close.png" );
            halfsize = 16;
        }
        else {
            closePic = QPixmap( ":/images/oxygen/dialog-close-small.png" );
            halfsize = 8;
        }

        WhaleButton * closeButton = new WhaleButton( central );
        closeButton->setPixmap( closePic );
        closeButton->move( centerx + ( imgw / 2.0 ) - halfsize, centery - ( imgh / 2.0 ) - halfsize );
        // closeButton->show();

        QPropertyAnimation * animation = new QPropertyAnimation( label, "size" );
        animation->setDuration( 500 );
        animation->setStartValue( QSize( 50, 50 ) );
        animation->setEndValue( QSize( imgw, imgh ) );
        animation->start( QAbstractAnimation::DeleteWhenStopped );

        // win->setDialogsLocked( true );

        this->connect( animation, SIGNAL( finished() ), closeButton, SLOT( show() ) );
        this->connect( closeButton, SIGNAL( clicked() ), label, SLOT( hide() ) );
        this->connect( closeButton, SIGNAL( clicked() ), closeButton, SLOT( hide() ) );

        // this->connect( closeButton, SIGNAL( clicked() ), win, SLOT( setDialogsLocked() ) );
    }
}

void WhaleAvatar::mousePressEvent( QMouseEvent * event ) {
    if ( event->button() != Qt::LeftButton ) {
        return;
    }

    /*
    MainWindow * win = qobject_cast< MainWindow * >( this->window() );
    if ( win->dialogsLocked() ) {
        return;
    }
    */

    /*
    if ( this->mImage.isNull() ) {
        return;
    }
    */

    QString bigAvatarUrl = this->mAvatarUrl.remove( "_normal" );
    QNetworkReply * reply = this->mConnector->get( QNetworkRequest( QUrl( bigAvatarUrl ) ) );
    Q_UNUSED( reply );
}
